﻿using MetaQuotes.MT5WebAPI;
using MT5Collect;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static MT5DataCollect.DBService;

namespace MT5DataCollect
{
    public class ReportServer
    {
        /// <summary>
        /// 一手的单位
        /// </summary>
        public decimal oneVolume = 10000m;
        Form1.ShowMsgDelegate ShowMsg;

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="_showMsgDelegate"></param>
        public void INI(Form1.ShowMsgDelegate _showMsgDelegate)
        {
            try
            {
                ShowMsg = _showMsgDelegate;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 获取统计时间
        /// </summary>
        /// <returns></returns>
        public string GetSettlementTime()
        {
            string time = "";
            string sql = "";
            try
            {
                sql = "select * from tb_system_config where menu_name='settlementTime' LIMIT 1";
                DataTable dt = DBHelper.ExecuteQuery(sql);
                if (dt.Rows.Count > 0)
                {
                    time = dt.Rows[0]["value"].ToString();
                }
                return time;
            }
            catch (Exception)
            {
                return time;
            }
        }

        /// <summary>
        /// 获取统计时间
        /// </summary>
        /// <returns></returns>
        public DateTime? GetLastSettlementTime()
        {
            DateTime? time = null;
            string sql = "";
            try
            {
                //先查询设置表中的数据
                sql = "select * from tb_system_config where menu_name='lastSettlementTime' LIMIT 1";
                DataTable dt = DBHelper.ExecuteQuery(sql);
                if (dt.Rows.Count > 0)
                {
                    time = DateTime.Parse(dt.Rows[0]["value"].ToString());
                    return time;
                }
                return null;
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// 获取统计时间
        /// </summary>
        /// <returns></returns>
        public int SetLastSettlementTime(string value)
        {

            try
            {
                string sql = $"update tb_system_config set value='{value}' where menu_name='lastSettlementTime'";
                return DBHelper.ExecuteNonQuery(sql);
            }
            catch (Exception)
            {
                return 0;
            }
        }

        /// <summary>
        /// 获取所有推广者
        /// </summary>
        /// <returns></returns>
        public List<CrmUser> GetUser()
        {
            string sql = "";
            List<CrmUser> crmUserList = new List<CrmUser>();
            try
            {
                sql = "select crm_user.id,crm_user.login_name,crm_user.crm_up,crm_user.role,crm_user.scheme_id,mt5_user.login from crm_user join mt5_user on crm_user.id=mt5_user.crm_id";
                //需要确认账号锁定期间是否计算返佣
                //sql = "select crm_user.id,crm_user.crm_up,crm_user.role,crm_user.scheme_id,mt5_user.login from crm_user join mt5_user on crm_user.id=mt5_user.crm_id where mt5_user.type=0  and crm_user.state=1 and crm_up is not null and crm_up!=0";
                DataTable dt = DBHelper.ExecuteQuery(sql);
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        CrmUser crmUser = new CrmUser();
                        crmUser.id = int.Parse(row["id"].ToString());
                        if (string.IsNullOrEmpty(row["crm_up"].ToString()))
                        {
                            crmUser.up = 0;
                        }
                        else
                        {
                            crmUser.up = int.Parse(row["crm_up"].ToString());
                        }
                        if (string.IsNullOrEmpty(row["role"].ToString()))
                        {
                            crmUser.role = 0;
                        }
                        else
                        {
                            crmUser.role = int.Parse(row["role"].ToString());
                        }
                        if (string.IsNullOrEmpty(row["scheme_id"].ToString()))
                        {
                            crmUser.schemeId = 0;
                        }
                        else
                        {
                            crmUser.schemeId = int.Parse(row["scheme_id"].ToString());
                        }
                        if (string.IsNullOrEmpty(row["login"].ToString()))
                        {
                            crmUser.login = 0;
                        }
                        else
                        {
                            crmUser.login = int.Parse(row["login"].ToString());
                        }
                        crmUser.loginName = row["login_name"].ToString();
                        crmUserList.Add(crmUser);
                    }
                }
                return crmUserList;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 获取所有返佣设置
        /// </summary>
        /// <returns></returns>
        public List<Rebate> RebateLevel()
        {
            string sql = "";
            List<Rebate> rebateList = new List<Rebate>();
            try
            {
                sql = "select * from rebate_level where state=1";
                //获取所有返佣设置
                DataTable dt = DBHelper.ExecuteQuery(sql);
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        Rebate rebate = new Rebate();
                        rebate.id = int.Parse(row["id"].ToString());
                        rebate.schemeId = int.Parse(row["scheme_id"].ToString());
                        rebate.name = row["names"].ToString();
                        rebate.level = int.Parse(row["level"].ToString());
                        rebate.cardinal = string.IsNullOrEmpty(row["cardinal"].ToString())?0:decimal.Parse(row["cardinal"].ToString());
                        rebate.pid = int.Parse(row["pid"].ToString());
                        rebateList.Add(rebate);
                    }
                }
                return rebateList;
            }
            catch (Exception ex)
            {

                throw;
            }
        }

        /// <summary>
        /// 获取所有返佣方案
        /// </summary>
        /// <returns></returns>
        public List<RebateScheme> RebateScheme()
        {
            string sql = "";
            List<RebateScheme> rebateSchemeList = new List<RebateScheme>();
            try
            {
                sql = "select * from rebate_scheme";
                //获取所有返佣设置
                DataTable dt = DBHelper.ExecuteQuery(sql);
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        RebateScheme rebateScheme = new RebateScheme();
                        rebateScheme.id = int.Parse(row["id"].ToString());
                        rebateScheme.names = row["names"].ToString();
                        rebateSchemeList.Add(rebateScheme);
                    }
                }
                return rebateSchemeList;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 获取所有返佣设置
        /// </summary>
        /// <returns></returns>
        public List<Deal> Deal(DateTime startDt, DateTime endDt)
        {
            string sql = "";
            List<Deal> dealList = new List<Deal>();
            try
            {
                long startTime = GetTimestamp(startDt);
                long entTime = GetTimestamp(endDt);
                sql = $"select deal,login,time,symbol,REPLACE(FORMAT(volume, 0), ',', '') as volume,REPLACE(FORMAT(volumeClosed, 0), ',', '') as volumeClosed from mt5_deal where volumeClosed!=0 and time between '{startTime}' and '{entTime}' order by deal";
                //获取所有返佣设置
                DataTable dt = DBHelper.ExecuteQuery(sql);
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        try
                        {
                            Deal deal = new Deal();
                            deal.deal = long.Parse(row["deal"].ToString());
                            deal.login = long.Parse(row["login"].ToString());
                            deal.time = long.Parse(row["time"].ToString());
                            deal.symbol = row["symbol"] == null ? "" : row["symbol"].ToString().ToLower();
                            deal.volume = long.Parse(row["volume"].ToString());
                            deal.volumeClosed = long.Parse(row["volumeClosed"].ToString());
                            dealList.Add(deal);
                        }
                        catch (Exception)
                        {
                            //throw;
                        }
                    }
                }
                return dealList;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 返佣明细统计
        /// </summary>
        /// <param name="startDt"></param>
        /// <param name="endDt"></param>
        //public void Report(DateTime startDt, DateTime endDt)
        //{
        //    try
        //    {
        //        string time = GetSettlementTime();
        //        if (string.IsNullOrEmpty(time))
        //        {
        //            ShowMsg($"读取结算时间失败");
        //        }
        //        DateTime? lastTime = GetLastSettlementTime();
        //        if (lastTime==null)
        //        {
        //            lastTime = DateTime.Parse(DateTime.Today.ToString("yyyy-MM-dd") + " " + time);
        //            ShowMsg($"读取最后结算时间失败");
        //        }
        //        List<CrmUser> crmUserList = GetUser();
        //        ShowMsg($"读取用户{crmUserList.Count()}人");
        //        List<Rebate> rebateList = RebateLevel();
        //        ShowMsg($"读取返佣配置{rebateList.Count()}条");
        //        //返佣明细报表
        //        TimeSpan daysSpan = new TimeSpan(endDt.Ticks - startDt.Ticks);
        //        int days = daysSpan.Days;
        //        for (int i = 0; i < days; i++)
        //        {
        //            //返佣日期
        //            DateTime _dt = startDt.AddDays(i);
        //            //返佣开始时间
        //            DateTime _startDt = DateTime.Parse(_dt.ToString("yyyy-MM-dd") + " " + lastTime);
        //            //返佣结束时间
        //            DateTime _endDt = DateTime.Parse(_dt.AddDays(1).ToString("yyyy-MM-dd") + " " + time);
        //            DBService.DelDailyDetailedReport(_startDt.ToString("yyyy-MM-dd"));
        //            DBService.DelPersonReport(_startDt.ToString("yyyy-MM-dd"));
        //            DBService.DelDailyReport(_startDt.ToString("yyyy-MM-dd"));
        //            ShowMsg($"开始统计{_startDt.ToString("yyyy-MM-dd HH:mm:ss")}至{_endDt.ToString("yyyy-MM-dd HH:mm:ss")}的返佣");
        //            List<Deal> dealList = Deal(_startDt, _endDt);
        //            if (dealList.Count > 0)
        //            {
        //                CommissionDetailedReport(crmUserList, rebateList, dealList, _startDt, _endDt);
        //            }
        //        }

        //    }
        //    catch (Exception)
        //    {
        //        throw;
        //    }
        //}

        /// <summary>
        /// 返佣明细统计
        /// </summary>
        /// <param name="startDt"></param>
        /// <param name="endDt"></param>
        public void Report()
        {
            try
            {
                string time = GetSettlementTime();
                if (string.IsNullOrEmpty(time))
                {
                    ShowMsg($"读取结算时间失败");
                }
                DateTime? lastTime = GetLastSettlementTime();
                if (lastTime == null)
                {
                    //如果时间上次时间不存在，就获取最近一天
                    lastTime = DateTime.Parse(DateTime.Today.ToString("yyyy-MM-dd") + " " + time);
                    ShowMsg($"读取最后结算时间失败");
                }
                List<CrmUser> crmUserList = GetUser();
                ShowMsg($"读取用户{crmUserList.Count()}人");
                List<RebateScheme> rebateSchemeList = RebateScheme();
                List<Rebate> rebateList = RebateLevel();
                ShowMsg($"读取返佣配置{rebateList.Count()}条");
                //返佣开始时间
                DateTime _totalStartDt = (DateTime)lastTime;
                //返佣结束时间
                DateTime _totalEndDt = DateTime.Parse(DateTime.Today.ToString("yyyy-MM-dd") + " " + time);
                //返佣明细报表
                TimeSpan daysSpan = new TimeSpan(((DateTime)_totalEndDt.Date).Ticks - ((DateTime)_totalStartDt.Date).Ticks);
                int days = daysSpan.Days;
                for (int i = 0; i < days; i++)
                {
                    //返佣日期
                    DateTime _dt = _totalStartDt.Date.AddDays(i);
                    //返佣开始时间
                    DateTime _startDt = DateTime.Parse(_dt.ToString("yyyy-MM-dd") + " " + time);
                    if (i == 0)
                    {
                        //第一天使用最后结算时间
                        _startDt = _totalStartDt;
                    }
                    //返佣结束时间
                    DateTime _endDt = DateTime.Parse(_dt.AddDays(1).ToString("yyyy-MM-dd") + " " + time);
                    if (_endDt < DateTime.Now)
                    {
                        DBService.DelDailyDetailedReport(_dt.ToString("yyyy-MM-dd"));
                        DBService.DelPersonReport(_dt.ToString("yyyy-MM-dd"));
                        DBService.DelDailyReport(_dt.ToString("yyyy-MM-dd"));
                        ShowMsg($"开始统计{_startDt.ToString("yyyy-MM-dd HH:mm:ss")}至{_endDt.ToString("yyyy-MM-dd HH:mm:ss")}的返佣");
                        List<Deal> dealList = Deal(_startDt, _endDt);
                        if (dealList.Count > 0)
                        {
                            CommissionDetailedReport(crmUserList, rebateSchemeList, rebateList, dealList, _startDt, _endDt);
                        }
                        SetLastSettlementTime(_endDt.ToString("yyyy-MM-dd HH:mm:ss"));
                    }
                }
                ShowMsg($"返佣统计完成");
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 返佣明细统计
        /// </summary>
        /// <param name="startDt"></param>
        /// <param name="endDt"></param>
        public void CommissionDetailedReport(List<CrmUser> crmUserList, List<RebateScheme> rebateSchemeList, List<Rebate> rebateList, List<Deal> dealList, DateTime startDt, DateTime endDt)
        {
            try
            {

                List<CommissionDetailedReportMode> commissionDetailedReportModes = new List<CommissionDetailedReportMode>();
                //List<DailyDetailedReportMode> dailyDetailedReportModes = new List<DailyDetailedReportMode>();
                //抓取推广者
                List<CrmUser> promoters = crmUserList.Where(k => k.role == 1).ToList();
                if (crmUserList.Count > 0)
                {
                    //遍历每一位推广者
                    foreach (CrmUser crmUser in promoters)
                    {
                        string orderId = startDt.ToString("yyyyMMddHHmmss") + endDt.ToString("yyyyMMddHHmmss") + crmUser.id;
                        //抓取一级下线
                        List<CrmUser> level1CrmList = crmUserList.Where(k => k.up == crmUser.id).ToList();
                        List<Rebate> rebate1 = rebateList.Where(k => k.schemeId == crmUser.schemeId && k.level == 1).ToList();
                        if (level1CrmList.Count >= 0)
                        {
                            foreach (CrmUser level1Crm in level1CrmList)
                            {
                                //遍历一级下线交易
                                List<Deal> level1DealList = dealList.Where(k => k.login == level1Crm.login).ToList();
                                foreach (Deal level1Deal in level1DealList)
                                {
                                    Rebate rebate = rebate1.FirstOrDefault(k => k.name.ToLower() == level1Deal.symbol.ToLower());
                                    if (rebate != null)
                                    {
                                        RebateScheme rebateScheme = rebateSchemeList.FirstOrDefault(k => k.id == crmUser.schemeId);
                                        Rebate rebateLevel = rebate1.FirstOrDefault(k => k.pid == 0 && k.schemeId == crmUser.schemeId && k.level == 1);
                                        //返佣明细统计
                                        CommissionDetailedReportMode commissionDetailedReportMode1 = new CommissionDetailedReportMode();
                                        commissionDetailedReportMode1.owner = crmUser.id;
                                        commissionDetailedReportMode1.crmid = level1Crm.id;
                                        commissionDetailedReportMode1.crmlogin = level1Crm.loginName;
                                        commissionDetailedReportMode1.mt5login = level1Crm.login;
                                        commissionDetailedReportMode1.deal = level1Deal.deal;
                                        commissionDetailedReportMode1.volume = level1Deal.volumeClosed;
                                        commissionDetailedReportMode1.scheme_id = rebate.schemeId;
                                        commissionDetailedReportMode1.symbol = level1Deal.symbol;
                                        commissionDetailedReportMode1.level_id = rebate.level;
                                        commissionDetailedReportMode1.coefficient = rebate.cardinal;
                                        commissionDetailedReportMode1.money = level1Deal.volumeClosed / oneVolume * rebate.cardinal;
                                        commissionDetailedReportMode1.date = startDt.ToString("yyyy-MM-dd");
                                        commissionDetailedReportMode1.starttime = startDt.ToString("yyyy-MM-dd HH:mm:ss");
                                        commissionDetailedReportMode1.endtime = endDt.ToString("yyyy-MM-dd HH:mm:ss");
                                        commissionDetailedReportMode1.isreturn = 0;
                                        commissionDetailedReportMode1.order_id = orderId;
                                        commissionDetailedReportMode1.scheme = rebateScheme==null?"": rebateScheme.names;
                                        commissionDetailedReportMode1.level = rebateLevel == null ? "" : rebateLevel.name;
                                        //showMsg(JsonConvert.SerializeObject(commissionDetailedReportMode1));
                                        //DBService.CommissionDetailedReport(commissionDetailedReportMode1);
                                        commissionDetailedReportModes.Add(commissionDetailedReportMode1);

                                    }
                                }
                            }
                        }

                        //抓取二级下线
                        List<int> level1Id = level1CrmList.Select(k => k.id).ToList();
                        List<CrmUser> level2CrmList = crmUserList.Where(k => level1Id.Contains(k.up)).ToList();
                        List<Rebate> rebate2 = rebateList.Where(k => k.schemeId == crmUser.schemeId && k.level == 2).ToList();
                        if (level2CrmList.Count >= 0)
                        {
                            foreach (CrmUser level2Crm in level2CrmList)
                            {
                                //遍历二级下线交易
                                List<Deal> level2DealList = dealList.Where(k => k.login == level2Crm.login).ToList();
                                foreach (Deal level2Deal in level2DealList)
                                {
                                    Rebate rebate = rebate2.FirstOrDefault(k => k.name.ToLower() == level2Deal.symbol.ToLower());
                                    if (rebate != null)
                                    {
                                        RebateScheme rebateScheme = rebateSchemeList.FirstOrDefault(k => k.id == crmUser.schemeId);
                                        Rebate rebateLevel = rebate2.FirstOrDefault(k => k.pid == 0 && k.schemeId == crmUser.schemeId && k.level == 2);
                                        //返佣明细统计
                                        CommissionDetailedReportMode commissionDetailedReportMode2 = new CommissionDetailedReportMode();
                                        commissionDetailedReportMode2.owner = crmUser.id;
                                        commissionDetailedReportMode2.crmid = level2Crm.id;
                                        commissionDetailedReportMode2.crmlogin = level2Crm.loginName;
                                        commissionDetailedReportMode2.mt5login = level2Crm.login;
                                        commissionDetailedReportMode2.deal = level2Deal.deal;
                                        commissionDetailedReportMode2.volume = level2Deal.volumeClosed;
                                        commissionDetailedReportMode2.scheme_id = rebate.schemeId;
                                        commissionDetailedReportMode2.symbol = level2Deal.symbol;
                                        commissionDetailedReportMode2.level_id = rebate.level;
                                        commissionDetailedReportMode2.coefficient = rebate.cardinal;
                                        commissionDetailedReportMode2.money = level2Deal.volumeClosed / oneVolume * rebate.cardinal;
                                        commissionDetailedReportMode2.date = startDt.ToString("yyyy-MM-dd");
                                        commissionDetailedReportMode2.starttime = startDt.ToString("yyyy-MM-dd HH:mm:ss");
                                        commissionDetailedReportMode2.endtime = endDt.ToString("yyyy-MM-dd HH:mm:ss");
                                        commissionDetailedReportMode2.isreturn = 0;
                                        commissionDetailedReportMode2.order_id = orderId;
                                        commissionDetailedReportMode2.scheme = rebateScheme == null ? "" : rebateScheme.names;
                                        commissionDetailedReportMode2.level = rebateLevel == null ? "" : rebateLevel.name ;
                                        commissionDetailedReportModes.Add(commissionDetailedReportMode2);
                                    }
                                }
                            }
                        }
                    }
                }

                if (commissionDetailedReportModes.Count > 0)
                {
                    foreach (CommissionDetailedReportMode item in commissionDetailedReportModes)
                    {
                        ShowMsg(JsonConvert.SerializeObject(item));
                        DBService.CommissionDetailedReport(item);
                    }
                }
                List<DailyDetailedReportMode> dailyDetailedReportGroup = commissionDetailedReportModes.GroupBy(k => new { k.owner, k.symbol, k.scheme_id, k.level_id, k.coefficient, k.date,k.scheme,k.level }).Select(x => new DailyDetailedReportMode
                {
                    owner = x.Key.owner,
                    symbol = x.Key.symbol,
                    volume = x.Sum(k => k.volume),
                    scheme_id = x.Key.scheme_id,
                    level_id = x.Key.level_id,
                    scheme = x.Key.scheme,
                    level = x.Key.level,
                    coefficient = x.Key.coefficient,
                    money = x.Sum(k => k.money),
                    date = x.Key.date,
                    starttime = startDt.ToString("yyyy-MM-dd HH:mm:ss"),
                    endtime = endDt.ToString("yyyy-MM-dd HH:mm:ss")
                }).ToList();
                if (dailyDetailedReportGroup.Count > 0)
                {
                    foreach (DailyDetailedReportMode item in dailyDetailedReportGroup)
                    {
                        //ShowMsg(JsonConvert.SerializeObject(item));
                        DBService.DailyDetailedReport(item);
                    }
                }

                List<PersonReportMode> personReportGroup = commissionDetailedReportModes.GroupBy(k => new { k.owner, k.crmid, k.crmlogin, k.mt5login, k.scheme_id, k.level_id, k.coefficient, k.money, k.date, k.scheme, k.level }).Select(x => new PersonReportMode
                {
                    owner = x.Key.owner,
                    crmid = x.Key.crmid,
                    crmlogin = x.Key.crmlogin,
                    mt5login = x.Key.mt5login,
                    scheme_id = x.Key.scheme_id,
                    level_id = x.Key.level_id,
                    scheme = x.Key.scheme,
                    level = x.Key.level,
                    coefficient = x.Key.coefficient,
                    money = x.Sum(k => k.money),
                    date = x.Key.date,
                    starttime = startDt.ToString("yyyy-MM-dd HH:mm:ss"),
                    endtime = endDt.ToString("yyyy-MM-dd HH:mm:ss")
                }).ToList();
                if (personReportGroup.Count > 0)
                {
                    foreach (PersonReportMode item in personReportGroup)
                    {
                        //ShowMsg(JsonConvert.SerializeObject(item));
                        DBService.PersonReport(item);
                    }
                }

                List<DailyReportMode> dailyReportGroup = commissionDetailedReportModes.GroupBy(k => new { k.owner, k.date }).Select(x => new DailyReportMode
                {
                    owner = x.Key.owner,
                    count = x.Count(),
                    money = x.Sum(k => k.money),
                    date = x.Key.date,
                    starttime = startDt.ToString("yyyy-MM-dd HH:mm:ss"),
                    endtime = endDt.ToString("yyyy-MM-dd HH:mm:ss")
                }).ToList();
                if (dailyReportGroup.Count > 0)
                {
                    foreach (DailyReportMode item in dailyReportGroup)
                    {
                        //ShowMsg(JsonConvert.SerializeObject(item));
                        DBService.DailyReport(item);
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public long GetTimestamp(DateTime dt)
        {
            try
            {
                long timestamp = (long)(dt.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
                return timestamp;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public DateTime GetDatetime(long timestamp)
        {
            try
            {
                DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(timestamp);
                return dateTime;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 返佣至钱包
        /// </summary>
        public void CommissionToWallet()
        {
            try
            {
                string sql = "select order_id,`date`,`owner`,sum(money) as money FROM commission_detailed_report where isreturn=0 GROUP BY order_id,`date`";
                DataTable dt = DBHelper.ExecuteQuery(sql);
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        string orderid = row["order_id"].ToString();
                        string date = row["date"].ToString();
                        decimal money = decimal.Parse(row["money"].ToString());
                        int owner = int.Parse(row["owner"].ToString());
                        ShowMsg($"{orderid}|{date}|{owner}|{money}");
                        DBService.CommissionToWallet(orderid, date, owner, money);
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
    }

    public class CrmUser
    {
        /// <summary>
        /// CMRID
        /// </summary>
        public int id { get; set; }
        /// <summary>
        /// 上家CRMID
        /// </summary>
        public int up { get; set; }
        /// <summary>
        /// 0普通用户1推广
        /// </summary>
        public int role { get; set; }
        /// <summary>
        /// 返佣方案ID
        /// </summary>
        public int schemeId { get; set; }
        /// <summary>
        /// 交易账号
        /// </summary>
        public int login { get; set; }
        public string loginName { get; set; }
    }

    public class RebateScheme
    {
        /// <summary>
        /// 返佣方案
        /// </summary>
        public int id { get; set; }
        /// <summary>
        /// 返佣方案
        /// </summary>
        public string names { get; set; }
    }

    public class Rebate
    {
        /// <summary>
        /// 返佣方案ID
        /// </summary>
        public int id { get; set; }
        /// <summary>
        /// 返佣方案ID
        /// </summary>
        public int schemeId { get; set; }
        /// <summary>
        /// 返佣币种
        /// </summary>
        public string name { get; set; }
        /// <summary>
        /// 返佣等级
        /// </summary>
        public int level { get; set; }
        /// <summary>
        /// 返佣基数
        /// </summary>
        public decimal cardinal { get; set; }
        public int pid { get; set; }
    }

    public class Deal
    {
        /// <summary>
        /// 交易单号
        /// </summary>
        public long deal { get; set; }
        /// <summary>
        /// 交易账号
        /// </summary>
        public long login { get; set; }
        /// <summary>
        /// 交易执行时间
        /// </summary>
        public long time { get; set; }
        /// <summary>
        /// 交易执行所针对的交易品种
        /// </summary>
        public string symbol { get; set; }
        /// <summary>
        /// 交易量
        /// </summary>
        public long volume { get; set; }
        /// <summary>
        /// 获得和设置由交易平仓的持仓量
        /// </summary>
        public long volumeClosed { get; set; }
    }
}
